#!/bin/bash
 

NAME="easycontrol_monitor"

DESC="Easycontrol Monitor"

EXEC_FILE="/usr/local/easycontrol/bin/monitor"
PID_FILE="/var/run/monitor.pid"
RM="/bin/rm"
 
start() {
    echo -n "Iniciando $DESC: "
    start-stop-daemon --start --quiet --background \
        --name ${NAME} --make-pidfile \
        --pidfile ${PID_FILE} --startas ${EXEC_FILE}
    echo "pronto."
}
 
stop() {
    echo -n "Parando $DESC: "
    start-stop-daemon --stop --quiet --name ${NAME} --pidfile ${PID_FILE}
    ${RM} -f ${PID_FILE}
    echo "pronto."
}

status(){  
    echo -n "Status de $DESC: "  
    if [ ! -r "$PID_FILE" ]; then  
    echo "$NAME nao esta rodando."  
        exit 3  
    fi  
    if read pid < "$PID_FILE" && ps -p "$pid" > /dev/null 2>&1; then  
        echo "$NAME esta rodando."  
        exit 0  
    else  
       echo "$NAME nao esta rodando mas $PID_FILE existe."  
       exit 1  
    fi  
}
 
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        stop
        start
        ;;
	"")
	start
	;;
	
        
    *)
        echo "Usage: $0 {start|stop|restart}"
        ;;
esac
 
exit 0
